<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
abstract class Mm_Xml_Element_Parent {
    
    protected $childElements = array();
    
    protected $tagname = "";
    
    private $namespaces = array();
    
    protected function __construct(array $childElements) {
        $this->childElements = $childElements;
    }
    
    public function getChildElement($key) {
        if(isset($this->childElements[$key])) {
            return $this->childElements[$key];
        }
        return null;
    }
    
    public function addChildElement($key, $childElements) {
        $this->childElements[$key] = $childElements;
    }
    
    public function setNamespaces(array $namespaces) {
        $this->namespaces = $namespaces;
    }
    
    /**
     * 
     * @return string
     */
    public function asXML() {
        $domDoc = new DOMDocument('1.0', 'UTF-8');
        $domDoc->formatOutput = true;
        
        $root_element = $domDoc->createElement($this->tagname);
        
        reset($this->namespaces);
        foreach ($this->namespaces as $k => $v) {
            if(empty($k)) {
                    $root_element->setAttribute("xmlns", $v);
                } else {
                    $root_element->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:" . $k, $v);
                }
        }
        
        reset($this->childElements);
        foreach ($this->childElements as $childElement) {
            if(is_array($childElement)) {
                foreach ($childElement as $element) {
                    $root_element->appendChild($element->asDomElement($domDoc));
                }
            } else {
                $root_element->appendChild($childElement->asDomElement($domDoc));    
            }
        }
        
        $domDoc->appendChild($root_element);
        return $domDoc->saveXML();
    }
    
    /**
     * @param $domDoc DOMDocument
     * @return $elm DOMElement
     */
    public function asDomElement(DOMDocument $domDoc) {
        $elm = $domDoc->createElement($this->tagname);
        foreach ($this->childElements as $childElement) {
            $elm->appendChild($childElement->asDomElement($domDoc));    
        }
        return $elm;
    }
    
}